package cn.pxy.ysframework.web.Controller;

import cn.pxy.ysframework.utils.Message;
import cn.pxy.ysframework.web.Dto.CreateCardDto;
import cn.pxy.ysframework.web.Entity.Account;
import cn.pxy.ysframework.web.Entity.Card;
import cn.pxy.ysframework.web.Entity.Category;
import cn.pxy.ysframework.web.Service.IAccountService;
import cn.pxy.ysframework.web.Service.ICardService;
import cn.pxy.ysframework.web.Service.ICategoryService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;

@RestController
@RequestMapping(value = "/Card")
@Api(description = "帖子控制器")
public class CardController {

    @Autowired
    private ICardService cardService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IAccountService accountService;

/*    @PostMapping(value = "/test")
    public Message test(String id){
        return new Message(JSON.toJSONString(categoryService.findById(id)));
    }*/

    /**
     * 发帖
     * @param carddto 帖子DTO
     * @return 帖子实体
     */
    @PostMapping(value = "/CreateCard")
    @ApiOperation(value = "发帖--完成")
    public Message CreateCard(@RequestBody CreateCardDto carddto){
        Card card = new Card();
        card.setId(carddto.getId());
        card.setClickcount(0l);
        Account account = accountService.findById(carddto.getAccount_Id());
        Category category = categoryService.findById(carddto.getCategory_Id());
        card.setAccount(account);
        card.setCategory(category);

        card.setIsrecommend(0);
        card.setIstop(0);
        card.setHot(1l);

        card.setTitle(carddto.getTitle());
        card.setContent(carddto.getContent());

        card.setPublictime(new Date(System.currentTimeMillis()));

        return new Message(JSON.toJSONString(card));
    }

    /**
     * 分页查找帖子
     * @param page 第几页
     * @param num 返回多少条
     * @return
     */
    @PostMapping(value = "/FindAllCardWithPager")
    @ApiOperation(value = "分页查询帖子--完成")
    public Message FindAllCardWithPager(int page,int num){
        return new Message(JSON.toJSONString(cardService.findAllCardWithPager(page,num)));
    }

    /**
     * 分页查找帖子
     * @param id 帖子id
     * @return 帖子实体，包含评论。
     */
    @PostMapping(value = "/FindCardById")
    @ApiOperation(value = "根据Id获取帖子--完成")
    public Message FindCardById(String id){
        return new Message(JSON.toJSONString(cardService.findById(id)));
    }
}
